<?PHP

namespace SDF\Lib\Log;

use SDF\Common\Utils;
use SDF\Db\MongoDb;

/**
 * 基于Mongo数据库的日志类
 * @package SDF\Lib\Log
 */
class log extends MongoDb {
    /**
     * 授权构造
     * auth_console constructor.
     * @param null $collection
     */
    public function __construct($collection = null){
        parent::__construct($collection,'log');
    }

    /**
     * 写入标准格式的日志数据
     * @param string $type 日志类型  INFO DEBUG ERROR等
     * @param string $message
     * @param int $code
     * @param array $trace
     * @param string $out_trade_no  商户支付单id
     * @return array|\MongoDB\Driver\WriteResult
     */
    public function write($type, $message = '', $code = 0, $trace = array(), $out_trade_no = '') {
        $data = array(
            'time'=>time(),
            'Ymd'=>date('Ymd'),
            'type'=>strtoupper($type),
            'code'=>$code,
            'trace'=>$trace,
            'message'=>$message,
            'out_trade_no' => $out_trade_no.'',
            'ip' => Utils::GetIP()
        );
        try{
            $res = $this->insertOne($data);
            return $res;
        }catch (\Exception $e){
            echo $e->getMessage();
            return array('ok' => 0);
        }
    }
}